package views;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenu;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;

import model.*;

import java.awt.Font;

import javax.swing.UIManager;
import javax.swing.SwingConstants;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;

import services.ClientesServices;

import java.awt.CardLayout;
import java.awt.SystemColor;
import java.awt.GridLayout;

import net.miginfocom.swing.MigLayout;

public class PanelVerClientes extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTable table;

	
	private void EscribiryLeerClientes(){

		/*
		// FECHA DE ALTA PRUEBA
		Date a = new Date();
		a.setYear(2014);
		a.setMonth(4);
		a.setDate(2);

		
		//########### 1 #######
		// ESCRIBIR CLIENTES DE PRUEBA
		ObjectOutputStream saveclient = null;
		try {
			saveclient = new ObjectOutputStream(new BufferedOutputStream(
					new FileOutputStream("clientes.txt")));
			Clientes c1 = new Clientes(1, "Julieta", a);
			Clientes c2 = new Clientes(2, "Ignacio", a);
			Clientes c3 = new Clientes(3, "Jesus", a);
			
			List<Clientes> qs = Arrays.asList(c1, c2, c3);
			saveclient.writeObject(qs);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {

			try {
				saveclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		//########### 2 #######
		 */
		
		// LEER CLIENTES
		ObjectInputStream dos = null;
		try {

			dos = new ObjectInputStream(new BufferedInputStream(
					new FileInputStream("clientes.txt")));

			while (true) {

				try {

					List<Clientes> Clientestxt = (List<Clientes>) dos.readObject();
					// display its data
					for (Clientes q : Clientestxt) {
						System.out.println(q.getNombre());
						DefaultTableModel defaultModel = (DefaultTableModel) table
								.getModel();
						Vector row = new Vector();
						row.add(q.getId());
						row.add(q.getNombre());
						row.add(q.getUltimoIngreso().toString());
						defaultModel.addRow(row);
						row = null;
						// table = defaultModel
						
						
						
						//INSERTAR CLIENTE EN BASE
							
						
						//Punto3
						//GRABAMOS EN LA BASE TAMBIEN
						Clientes cli = new Clientes(q.getNombre(), "", "", "","");
						try {
							new ClientesServices().SaveCliente(cli);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					
					
					
						System.out.println("Cliente Cargado: " + q.getNombre());

					}

				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		} catch (Exception e) {

		} finally {
			System.out.println("Cargando Clientes");
			if (dos != null) {

				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}

	}
	public PanelVerClientes() {
		

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.control);
		//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// GENERAL
		//setTitle("Asistente de Viajes - Ver Clientes");
		setSize(new Dimension(911, 605));
		//setOpacity(01);

		// FECHA
		// System.out.println(a.toString());

		// IMG
		BufferedImage myPicture;
		panel_1.setBounds(10, 11, 545, 569);
		add(panel_1);
						panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
				
						// PARTICULAR
				
						JPanel panel = new JPanel();
						
						panel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
					//	panel.setAlignmentX(CENTER_ALIGNMENT);
						panel_1.add(panel);
																				panel.setLayout(new MigLayout("", "[468.00px]", "[38.00px][]"));
								//panel_2.setAlignmentX(CENTER_ALIGNMENT);
								
																				JLabel lblClientesActuales = new JLabel("Clientes Actuales");
																				lblClientesActuales.setAlignmentX(Component.CENTER_ALIGNMENT);
																				panel.add(lblClientesActuales, "cell 0 0,alignx center,aligny top");
																				lblClientesActuales.setAlignmentY(Component.TOP_ALIGNMENT);
																				lblClientesActuales.setVerticalAlignment(SwingConstants.TOP);
																				
																					lblClientesActuales.setFont(new Font("Tahoma", Font.PLAIN, 16));

		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane tableSP = new JScrollPane(table);
		tableSP.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		panel.add(tableSP, "cell 0 1,growx,aligny bottom");
		
				tableSP.setBorder(new EmptyBorder(5, 5, 5, 5));
				
						table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
								"ID", "Nombre", "UltimoIngreso" }) {
							Class[] columnTypes = new Class[] { String.class, String.class,
									Integer.class };
				
							public Class getColumnClass(int columnIndex) {
								return columnTypes[columnIndex];
							}
						});
						
								table.getTableHeader().setReorderingAllowed(false);// no reordenar
								// columnas
table.setAutoResizeMode(JTable.WIDTH);// que ocupe todo lo del jpanel
EscribiryLeerClientes();
	}

}
